package com.gmcc.docmanager.view;

import com.gmcc.docmanager.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface.OnClickListener;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomerDialogBuilder {
	private Activity mActivity = null;
	private LayoutInflater mInflater;
	private Dialog mDialog;
	private View rootView;
	private TextView dialogTitleText;
	private TextView dialogMsgText;
	private Button leftButton;
	private Button rightButton;
	private LinearLayout dialogContentLayout;

	private boolean isCustomerView = false;

	public CustomerDialogBuilder(Activity activity) {
		mActivity = activity;
		mInflater = LayoutInflater.from(activity);
		init();
	}

	private void init() {
		rootView = mInflater.inflate(R.layout.dialog_layout_wood, null);
		dialogContentLayout = (LinearLayout) rootView
				.findViewById(R.id.dialogContentLayout);
		dialogTitleText = (TextView) rootView
				.findViewById(R.id.dialogTitleText);
		dialogMsgText = (TextView) rootView.findViewById(R.id.dialogMsgText);
		leftButton = (Button) rootView.findViewById(R.id.dialogLeftBtn);
		rightButton = (Button) rootView.findViewById(R.id.dialogRightBtn);
		mDialog = new Dialog(mActivity, R.style.PopContextMenu);
	}

	public CustomerDialogBuilder setTitle(int resId) {
		dialogTitleText.setText(resId);
		return this;
	}

	public CustomerDialogBuilder setMessage(int resId) {
		dialogMsgText.setText(resId);
		return this;
	}

	public CustomerDialogBuilder setMessage(String msg) {
		dialogMsgText.setText(msg);
		return this;
	}

	public CustomerDialogBuilder setView(View view) {
		rootView = view;
		isCustomerView = true;
		return this;
	}

	public CustomerDialogBuilder setContentView(View view) {
		dialogMsgText.setVisibility(View.GONE);
		dialogContentLayout.addView(view, 0);
		return this;
	}

	public CustomerDialogBuilder setPositiveButton(int textId,
			final OnClickListener listener) {
		leftButton.setText(textId);
		if (listener != null) {
			leftButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					listener.onClick(mDialog, 0);
				}
			});
		}
		return this;
	}

	public CustomerDialogBuilder setNegativeButton(int textId,
			final OnClickListener listener) {
		rightButton.setVisibility(View.VISIBLE);
		rightButton.setText(textId);
		if (listener != null) {
			rightButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					listener.onClick(mDialog, 1);
				}
			});
		}
		return this;
	}

	public CustomerDialogBuilder setCancelable(boolean cancelable) {
		if (mDialog != null) {
			mDialog.setCancelable(cancelable);
		}
		return this;
	}

	public void show() {
		mDialog.setContentView(rootView);
		mDialog.show();
		if (!isCustomerView) {
			Window window = mDialog.getWindow();
			WindowManager.LayoutParams layoutParams = window.getAttributes();
			WindowManager wm = mActivity.getWindowManager();
			Display display = wm.getDefaultDisplay();
			layoutParams.width = (int) (display.getWidth() * 0.7);
			window.setAttributes(layoutParams);
		}
	}

	public void dismiss() {
		if (mDialog != null) {
			mDialog.dismiss();
		}
	}
}
